//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for CategoryForm.
	/// </summary>
	public class TokenForm : BaseBlogPage
	{
		protected TextBox TextPartBox;
		protected TextBox TextDescBox;
		protected TextBox LinkBox;
		protected RequiredFieldValidator TextPartValidator;
		protected RegularExpressionValidator TextPartRegexValidator;
		protected RequiredFieldValidator TextValidator;
		protected ResourceLinkButton SaveButton;
	
		override protected void OnInit(EventArgs e)
		{
			this.SaveButton.Click += new EventHandler(this.SaveButton_Click);
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
            if(!IsPostBack)
            {

				TextPartValidator.Text = Components.ResourceManager.GetString("CP_Blog_TextParts_TextPartName_Required");
				TextPartRegexValidator.Text = Components.ResourceManager.GetString("CP_Blog_TextParts_TextPartName_Validate");
				TextValidator.Text = ResourceManager.GetString("CP_Blog_TextParts_Text_Required");

            	TextToken token = GetToken(TokenID);
				if(token == null)
					token = new TextToken();

				TextPartBox.Text = token.Token;
				LinkBox.Text = token.Link;
				TextDescBox.Text = token.Text;
            }

			Head.AddTitle(ResourceManager.GetString("CP_Blog_TextParts_Editor_Title"), this.Context);
		}

		protected int TokenID
		{
			get
			{
				return CSContext.Current.GetIntFromQueryString("TokenID", -1);
			}
		}

        protected TextToken GetToken(int tokenID)
        {
			
			if(tokenID > 0)
			{
				ArrayList tokens = TextTokens.GetTokens(this.CurrentWeblog.SectionID);
				foreach(TextToken token in tokens)
				{
					if(token.TokenID == TokenID)	
						return token;
				}
			}

			return null;
        }


		private void SaveButton_Click(object sender, EventArgs e)
		{
            if(Page.IsValid)
            {
				TextToken token = GetToken(TokenID);
				if(token == null)
				{
					token = new TextToken();
					token.SectionID = this.CurrentWeblog.SectionID;

				}

				token.Token = TextPartBox.Text;
				token.Link = Globals.IsNullorEmpty(LinkBox.Text) ? null : LinkBox.Text;
				token.Text = TextDescBox.Text;

				if(token.TokenID > 0)
					TextTokens.Update(token);
				else
					TextTokens.Add(token);

				Modal.ClosePage(this.Page,"true");
                
            }
			
		}
	}
}
